package com.fitness.fitness.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fitness.fitness.convertor.NewsConvertor;
import com.fitness.fitness.modal.news.dto.NewsDto;
import com.fitness.fitness.modal.news.entity.News;
import com.fitness.fitness.modal.news.params.NewsPageQueryParams;
import com.fitness.fitness.modal.news.vo.NewsVO;
import com.fitness.fitness.modal.response.ServerResponseEntity;
import com.fitness.fitness.service.INewsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/news")
public class NewsController {

    private final INewsService newsService;
    private final NewsConvertor newsConvertor;
    
    /**
     * 分页获取
     */
    @GetMapping("/page")
    public ServerResponseEntity<IPage<NewsVO>> page(com.fitness.fitness.modal.news.params.NewsPageQueryParams queryParams) {
        Page<NewsVO> result = newsService.getNewsPage(queryParams);
        return ServerResponseEntity.success(result);
    }

    /**
     * 新增
     * @param equipmentDto
     * @return
     */
    @PostMapping("/add")
    public ServerResponseEntity<Boolean> add(@RequestBody NewsDto equipmentDto) {
        boolean result = newsService.addNews(equipmentDto);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("C10001","添加失败");
    }

    /**
     * 修改
     * @param equipmentDto
     * @return
     */
    @PutMapping("/update")
    public ServerResponseEntity<Boolean> update(@RequestBody NewsDto equipmentDto) {
        boolean result = newsService.updateNews(equipmentDto);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("C10002","更新失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public ServerResponseEntity<Boolean> delete(@RequestBody Long id) {
        boolean result = newsService.removeById(id);

        if(result) {
            return ServerResponseEntity.success(true);
        }
        return ServerResponseEntity.fail("C10003","删除失败");
    }

    /**
     * 获取课程表单
     */
    @GetMapping("/getNewsForm")
    public ServerResponseEntity<NewsVO> getNewsForm(Long id) {
        News result = newsService.getById(id);
        return ServerResponseEntity.success(newsConvertor.entity2Vo(result));
    }
}
